package com.dlc.yunguang.activitynew;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.commonlibrary.utils.TimeFormats;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.adapter.OrderDetailAdapter1;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.OrderDetailBean;
import com.dlc.yunguang.net.exception.ErrorMsgException;
import com.dlc.yunguang.net.protocol.StockProto;
import com.dlc.yunguang.net.protocol.base.HttpProtocol;
import com.dlc.yunguang.utils.BluetoothUtil;
import com.dlc.yunguang.R;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class LaundryOutboundDetailActivity extends BaseActivity {


    OrderDetailBean orderDetailBean;
    OrderDetailAdapter1 mOrderDetailAdapter;
    @BindView(R.id.wash_goods_hint)
    TextView washGoodsHint;
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.company)
    TextView company;
    @BindView(R.id.send_wash_code)
    TextView sendWashCode;
    @BindView(R.id.nums)
    TextView nums;
    @BindView(R.id.send_wash_time)
    TextView sendWashTime;
    @BindView(R.id.reveice_time)
    TextView reveiceTime;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.print)
    Button print;
    @BindView(R.id.sure)
    Button sure;
    private boolean isBold, isUnderLine;
    private int record;
    int type;



    @Override
    protected int getLayoutID() {
        return R.layout.activity_laundry_outbound_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftExit(this);

        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("type");
        orderDetailBean = (OrderDetailBean) bundle.getSerializable("detailData");

        if (orderDetailBean.data.spec_group.size() > 0) {
            InitRecyclerView(orderDetailBean);
            company.setText(orderDetailBean.data.company);
        }
        if (orderDetailBean.data.orderno.contains("W")) {
            washGoodsHint.setText(getString(R.string.send_wash_code));
        } else if (orderDetailBean.data.orderno.contains("D")) {
            washGoodsHint.setText(getString(R.string.send_goods_code));
        } else if (orderDetailBean.data.orderno.contains("T")) {
            washGoodsHint.setText("退貨單號");
        } else {
            washGoodsHint.setText("返洗單號");
        }
        sendWashTime.setText(
                TimeFormats.formatDateTimeSecend((long) Integer.parseInt(orderDetailBean.data.ctime) * 1000));
        if (TextUtils.isEmpty(orderDetailBean.data.receivetime) || orderDetailBean.data.receivetime.equals("0")) {
            reveiceTime.setText(getString(R.string.not_yet_received));
        } else {
            reveiceTime.setText(TimeFormats.formatDateTimeSecend((long) Integer.parseInt(orderDetailBean.data.receivetime) * 1000));
        }
        sendWashCode.setText(orderDetailBean.data.orderno);
        nums.setText(orderDetailBean.data.nums);


        isBold = false;
        isUnderLine = false;
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                order_wash_receive();
            }
        });
        print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = "";
                //  printB();
                String orderno = orderDetailBean.data.orderno;
                string = "";
                string = string + "  海恩康（天津）医疗科技有限公司" + "\r\n";
                if (orderno.contains("W")) {
                    string = string + "         " + getString(R.string.washing_oeder) + "\r\n";
                } else if (orderno.contains("D")) {
                    string = string + "          " + getString(R.string.receiving_note) + "\r\n";
                }

                string = string + "单    号：" + orderno + "\r\n";
                string = string + "名    称：" + orderDetailBean.data.hotel + "\r\n";
                if (!TextUtils.isEmpty(orderDetailBean.data.office)) {
                    string = string + "医院科室：" + orderDetailBean.data.office + "\r\n";
                }
                string = string + "总 数 量：" + orderDetailBean.data.nums + "\r\n";

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date curDate = new Date(System.currentTimeMillis());
                String date = formatter.format(curDate);
                string = string + "日    期：" + date + "\r\n";

                string = string + "-------------------------";

                for (int i = 0; i < orderDetailBean.data.spec_group.size(); i++) {
                    OrderDetailBean.DataBean.SpecGroupBean bean = orderDetailBean.data.spec_group.get(i);
                    string = string + "\r\n" + (bean.spec + "   " + bean.count);
                    string = string + "\r\n" +("人员：" +bean.user);
                    string = string + "\r\n";
                }
                string = string + "-------------------------";
                string = string + "\r\n";
                string = string + "备注：" + "\r\n" ;
                string = string + "□ 污染"+ "\r\n";
                string = string + "□ 破损";
                string = string + "\r\n"+ "\r\n";
                string = string + getString(R.string.signature) + "：" + "\r\n" + "\r\n" + "\r\n";
                string = string + "-------------------------";
                string = string + "\r\n"+ "\r\n";

                //rv = BytesUtil.getBytesFromHexString();
                //AidlUtil.getInstance().sendRawData(BytesUtil.getBytesFromHexString(text));
                //sendData(rv);
                Intent intent = new Intent(getActivity(), SearchBTActivity.class);
                intent.putExtra(SearchBTActivity.key_string, string);
                startActivity(intent);
                //sendData(string, 22);
            }
        });
    }


    private void InitRecyclerView(OrderDetailBean orderDetailBean) {
        mOrderDetailAdapter = new OrderDetailAdapter1();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mOrderDetailAdapter);
        if (orderDetailBean.data.spec_group != null) {
            mOrderDetailAdapter.addData(orderDetailBean.data.spec_group);
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        BluetoothUtil.disconnectBlueTooth(this);
    }



    private void order_wash_receive() {

        if (TextUtils.isEmpty( orderDetailBean.data.rfids)){
            Toast.makeText(getActivity(), "订单异常，RFID为空", Toast.LENGTH_SHORT).show();
            return;
        }
        showWaitingDialog("请等待", true);
        StockProto.get()
                .order_wash_receive(orderDetailBean.data.orderno, orderDetailBean.data.rfids, new HttpProtocol.Callback<OrderDetailBean>() {
                    @Override
                    public void onSuccess(OrderDetailBean orderDetailBean) {
                        dismissWaitingDialog();
                        Toast.makeText(getActivity(), orderDetailBean.msg, Toast.LENGTH_SHORT).show();
                        sure.setVisibility(View.GONE);
                    }

                    @Override
                    public void onFailed(int code, ErrorMsgException e) {
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        dismissWaitingDialog();
                    }
                });
    }

}
